package com.android.logcat.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;

import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import androidx.annotation.Nullable;
import com.android.logcat.LoggerConfig.BindLoggerAidlStub;
import com.android.logcat.R;
import wei.mark.standout.StandOutWindow;
import wei.mark.standout.constants.StandOutFlags;
import wei.mark.standout.ui.Window;

/**
 * @author GYL
 * @version 1.0
 * @dateTime 2020/7/1-17:18
 * @description Floating view to show logcat logs
 */
public class LoggerFloatWindow extends StandOutWindow {
  //Views
  private ListView mListView;
  private LogViewrListAdapter mAdapter;
 
  @SuppressLint("HandlerLeak")
  private Handler mHandler = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(Message msg) {
      if(msg!=null){
       if(mAdapter!=null){
         mAdapter.add(String.valueOf(((char)msg.arg1)),String.valueOf(msg.obj));
       }
      }
    }
  };
  
  @SuppressLint("NewApi")
  @Override
  public String getAppName() {
    return getString(getApplicationInfo().labelRes);
  }
  
  @SuppressLint("NewApi")
  @Override
  public int getAppIcon() {
    return getApplicationInfo().icon;
  }
  
  @Override
  public void onCreate() {
    super.onCreate();
  }
  
  @Override
  public void onDestroy() {
    super.onDestroy();
  }
  
  @Override
  public void createAndAttachView(int id, FrameLayout frame) {
    // create a new layout from body.xml
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    final View rootView = inflater.inflate(R.layout.view_logger_floatwindow, frame, true);
    setupLogListView(rootView);
  
    ViewParent parent = frame.getParent();
    if(parent!=null && parent instanceof RelativeLayout){
      View hideView=((RelativeLayout) parent).findViewById(R.id.hide);
      if(hideView!=null){
        hideView.setVisibility(View.GONE);
      }
      View closeView=((RelativeLayout) parent).findViewById(R.id.close);
      if(closeView!=null){
        closeView.setVisibility(View.GONE);
      }
    }
  }
  
  // the window will be centered
  @Override
  public StandOutLayoutParams getParams(int id, Window window) {
    DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
    return new StandOutLayoutParams(id, displayMetrics.widthPixels * 5 / 6, displayMetrics.heightPixels / 2,
      StandOutLayoutParams.RIGHT, StandOutLayoutParams.BOTTOM, 400, 300);
  }
  
  // move the window by dragging the view
  @Override
  public int getFlags(int id) {
    return StandOutFlags.FLAG_DECORATION_SYSTEM
             | StandOutFlags.FLAG_BODY_MOVE_ENABLE
             | StandOutFlags.FLAG_WINDOW_HIDE_ENABLE
             | StandOutFlags.FLAG_WINDOW_BRING_TO_FRONT_ON_TAP
             | StandOutFlags.FLAG_WINDOW_EDGE_LIMITS_ENABLE
             | StandOutFlags.FLAG_WINDOW_PINCH_RESIZE_ENABLE;
  }
  
  @Override
  public String getPersistentNotificationMessage(int id) {
    return "Show LogcatViewer floating view.";
  }
  
  @Override
  public Intent getPersistentNotificationIntent(int id) {
    return StandOutWindow.getShowIntent(this, LoggerFloatWindow.class, id);
  }
  
  /**
   * Setup list view to show logcat log-entries.
   * @param rootView root view.
   */
  private void setupLogListView(final View rootView){
    //Log entry list view
    mListView = (ListView) rootView.findViewById(R.id.list);
    mListView.setStackFromBottom(true);
    mListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
    mAdapter = new LogViewrListAdapter(getApplicationContext());
    mListView.setAdapter(mAdapter);
  }
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    this.init(intent);
    return new BindLoggerAidlStub(mHandler);
  }
 
}
